package com.manli.api.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.manli.common.utils.HttpUtil;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.SQLReady;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.List;
import com.manli.api.pojo.Result;
import com.manli.api.util.ResultUtil;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.manli.api.pojo.QueryParams;
import com.manli.api.dao.GoodMorningDao;
import com.manli.api.dto.GoodMorning;


@Service("goodMorningService")
public class GoodMorningService {
    @Autowired
    GoodMorningDao goodMorningDao;
    @Autowired
    SQLManager sqlManager;

    
    public Result queryPage(QueryParams params) {
        /**
        * sql中表名需要修改
        */
        StringBuffer sql = new StringBuffer("select * from goodMorning where 1=1 ");

        SQLReady sqlReady = new SQLReady(sql.toString());

        PageQuery<GoodMorning> pageQuery = new PageQuery<GoodMorning>(params.getPage(),params.getLimit());
        PageQuery<GoodMorning> query =sqlManager.execute(sqlReady,GoodMorning.class,pageQuery);
        return ResultUtil.success(query.getList(),query.getTotalRow());
    }

    
    public Result queryAll() {
        List<GoodMorning> list = goodMorningDao.all();
        return ResultUtil.success(list);
    }

    
    public Result getById(Integer id) {
        GoodMorning goodMorning = goodMorningDao.single(id);
        return ResultUtil.success(goodMorning);
    }

    @Transactional
    public Result save(GoodMorning goodMorning) {
        goodMorningDao.insert(goodMorning);
        return ResultUtil.success();
    }

    @Transactional
    public Result updateById(GoodMorning goodMorning) {
        goodMorningDao.updateTemplateById(goodMorning);
        return ResultUtil.success();
    }

    @Transactional
	public Result updateTemplateById(GoodMorning goodMorning) {
        goodMorningDao.updateTemplateById(goodMorning);
        return ResultUtil.success();
    }

    @Transactional
    public Result removeByIds(List<Integer> ids) {
        for (Integer id:ids) {
            goodMorningDao.deleteById(id);
        }
        return ResultUtil.success();
    }


    public Result getTianxingSave() throws Exception{
        String url = "http://api.tianapi.com/txapi/zaoan/index?key=153f82138312ab9aaa64d2a9a495c5f5";

        for(int i=0;i<1001;i++){
            String result = HttpUtil.httpGet(url);
            System.out.println(result);
            JSONObject resultJson = JSONObject.parseObject(result);
            if(resultJson.getInteger("code").intValue() == 200){
                JSONArray array = resultJson.getJSONArray("newslist");
                JSONObject jsonObject = (JSONObject)array.get(0);

                List<GoodMorning> list = goodMorningDao.execute("select * from good_morning where text = ?",jsonObject.getString("content"));
                if(list.size() == 0){
                    GoodMorning goodMorning = new GoodMorning();
                    goodMorning.setText(jsonObject.getString("content"));
                    goodMorningDao.insert(goodMorning);
                }

            }
        }

        return ResultUtil.success();
    }

    public Result randOne() {
        List<GoodMorning> list = goodMorningDao.execute("SELECT * FROM good_morning ORDER BY RAND() LIMIT 1");
        return ResultUtil.success(list.get(0));
    }
}
